package org.scorpios.search.plugin;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.elasticsearch.index.analysis.AnalyzerProvider;
import org.elasticsearch.index.analysis.TokenizerFactory;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.plugins.AnalysisPlugin;
import org.elasticsearch.plugins.Plugin;
import org.scorpios.search.analyzer.SearchAnalyzerProvider;
import org.scorpios.search.analyzer.SearchTokenizerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangxudong
 * @date 2022/11/12 20:20
 * @desc
 */
public class SearchAnalyzerPlugin extends Plugin implements AnalysisPlugin {
    private final static Logger LOG = LogManager.getLogger(SearchAnalyzerPlugin.class);
    private static final String PLUGIN_NAME_FRENCH = "search";

    public SearchAnalyzerPlugin() {
        LOG.info("初始化插件....");
    }

    @Override
    public Map<String, AnalysisModule.AnalysisProvider<TokenizerFactory>> getTokenizers() {
        Map<String, AnalysisModule.AnalysisProvider<TokenizerFactory>> map = new HashMap<>();
        map.put(PLUGIN_NAME_FRENCH, SearchTokenizerFactory::new);
        return map;
    }

    @Override
    public Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends Analyzer>>> getAnalyzers() {
        Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends Analyzer>>> map = new HashMap<>();
        map.put(PLUGIN_NAME_FRENCH, SearchAnalyzerProvider::new);
        return map;
    }
}
